import api from '@/api';

export default {
  namespace: 'afterSale',

  state: {
    listData: {
      list: [],
      pagination: {}
    },
    status: [],
    dealTypes: [],
    todoCount: 0,
    duties: [],
    compensations: [],
    resultDetail: {},
  },

  effects: {
    *fetchListDatas({ payload }, { call, put }) {
      const { data: list, pagination, errorCode } = yield call(api.afterSale.list, payload);
      if (errorCode === 0) {
        yield put({
          type: 'saveListDatas',
          payload: {
            list,
            pagination
          },
        });
      }
    },
    *fetchStatus(_, { call, put }) {
      const res = yield call(api.afterSale.status);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveStatus',
          payload: res.data
        });
      }
    },
    *canApply({ payload }, { call }) {
      return yield call(api.afterSale.canApply, payload);
    },
    *apply({ payload }, { call }) {
      return yield call(api.afterSale.apply, payload);
    },
    *fetchDetail({ payload }, { call }) {
      return yield call(api.afterSale.details, payload);
    },
    *canReply({ payload }, { call }) {
      return yield call(api.afterSale.canReply, payload);
    },
    *reply({ payload }, { call }) {
      return yield call(api.afterSale.reply, payload);
    },
    *unable({ payload }, { call }) {
      return yield call(api.afterSale.unable, payload);
    },
    *todoCount({ payload }, { call, put }) {
      const res =  yield call(api.afterSale.todoNum, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveTodoCount',
          payload: res.data
        });
      }
    },
    *fetchDealType({ payload }, { call, put }) {
      const res = yield call(api.afterSale.dealType, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveDealType',
          payload: res.data
        });
      }
    },
    *fetchCompensations(_, { call, put }) {
      const res = yield call(api.afterSale.compensation);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCompensations',
          payload: res.data
        });
      }
    },
    *fetchDuties({ payload }, { call, put }) {
      const res = yield call(api.afterSale.duties, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveDuties',
          payload: res.data
        });
      }
    },
    *fetchResult({ payload }, { call, put }) {
      const res =  yield call(api.afterSale.resultDetail, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveResult',
          payload: res.data
        });
      }
    },
    *submitResult({ payload }, { call }) {
      return yield call(api.afterSale[payload.type], payload);
    },
    *coefficient({ payload }, { call }) {
      return yield call(api.afterSale.coefficient, payload);
    },
  },

  reducers: {
    saveListDatas(state, { payload }) {
      return {
        ...state,
        listData: payload
      };
    },
    saveStatus(state, { payload }) {
      return {
        ...state,
        status: payload,
      }
    },
    saveDealType(state, { payload }) {
      return {
        ...state,
        dealTypes: payload,
      }
    },
    saveTodoCount(state, { payload }) {
      return {
        ...state,
        todoCount: payload,
      }
    },
    saveDuties(state, { payload }) {
      return {
        ...state,
        duties: payload
      }
    },
    saveCompensations(state, { payload }) {
      return {
        ...state,
        compensations: payload
      }
    },
    saveResult(state, { payload }) {
      return {
        ...state,
        resultDetail: payload
      }
    },
  },
};
